﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;

namespace Capstone_project
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection con = Common.con;
        DataTable dt = new DataTable();
        private void Form1_Load(object sender, EventArgs e)
        {
            dt.Rows.Clear();
            string sql;
            if (Common.permission.Trim() == "Admin" || Common.permission.Trim()=="PM/PL")
            {
                sql = "SELECT     NULL AS Expr1, dbo.TASK.ID_TASK, dbo.TASK.NAME_TASK, dbo.PROJECT.NAME_PROJECT, dbo.TASK_GROUP.NAME_TASK_GROUP, "
                          + " dbo.PRIORITY.NAME_PRIORITY, dbo.TASK.TIMES, dbo.USER_TASK.START_DATE, dbo.USER_TASK.END_DATE, dbo.ASSIGN_TASK_STATUS.STATUS, "
                          + " dbo.USER_TASK.USER_NAME, dbo.USER_TASK.COMPLETE, dbo.USER_TASK.DESCRIPTION, 'Send' as Approve"
                            + " FROM         dbo.TASK INNER JOIN"
                          + " dbo.TASK_GROUP ON dbo.TASK.ID_TASK_GROUP = dbo.TASK_GROUP.ID_TASK_GROUP INNER JOIN"
                          + " dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT INNER JOIN"
                          + " dbo.PRIORITY ON dbo.TASK.ID_PRIORITY = dbo.PRIORITY.ID_PRIORITY INNER JOIN"
                          + " dbo.USER_TASK ON dbo.TASK.ID_TASK = dbo.USER_TASK.ID_TASK INNER JOIN"
                          + " dbo.ASSIGN_TASK_STATUS ON dbo.TASK.ID_TASK_STATUS = dbo.ASSIGN_TASK_STATUS.ID_TASK_STATUS";
            }
            else
            {
                sql = "SELECT     NULL AS Expr1, dbo.TASK.ID_TASK, dbo.TASK.NAME_TASK, dbo.PROJECT.NAME_PROJECT, dbo.TASK_GROUP.NAME_TASK_GROUP, "
                          + " dbo.PRIORITY.NAME_PRIORITY, dbo.TASK.TIMES, dbo.USER_TASK.START_DATE, dbo.USER_TASK.END_DATE, dbo.ASSIGN_TASK_STATUS.STATUS, "
                          + " dbo.USER_TASK.USER_NAME, dbo.USER_TASK.COMPLETE, dbo.USER_TASK.DESCRIPTION, 'Send' as Approve"
                            + " FROM         dbo.TASK INNER JOIN"
                          + " dbo.TASK_GROUP ON dbo.TASK.ID_TASK_GROUP = dbo.TASK_GROUP.ID_TASK_GROUP INNER JOIN"
                          + " dbo.PROJECT ON dbo.TASK_GROUP.ID_PROJECT = dbo.PROJECT.ID_PROJECT INNER JOIN"
                          + " dbo.PRIORITY ON dbo.TASK.ID_PRIORITY = dbo.PRIORITY.ID_PRIORITY INNER JOIN"
                          + " dbo.USER_TASK ON dbo.TASK.ID_TASK = dbo.USER_TASK.ID_TASK INNER JOIN"
                          + " dbo.ASSIGN_TASK_STATUS ON dbo.TASK.ID_TASK_STATUS = dbo.ASSIGN_TASK_STATUS.ID_TASK_STATUS"
                          + " WHERE USER_TASK.USER_NAME='"+Common.username[0].ToString()+"'";
            }
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand(sql,con);
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt.DefaultView;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor=Color.Red;
            Column14.DefaultCellStyle.BackColor = Color.SlateGray;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i % 2 == 0)
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
                }
                if (dt.Rows[i][5].ToString() == "Very low")
                {
                    dataGridView1.Rows[i].Cells[5].Style.BackColor = Color.AntiqueWhite;
                }
                else if (dt.Rows[i][5].ToString() == "Low")
                {
                    dataGridView1.Rows[i].Cells[5].Style.BackColor = Color.NavajoWhite;
                }
                else if (dt.Rows[i][5].ToString() == "Nomal")
                {
                    dataGridView1.Rows[i].Cells[5].Style.BackColor = Color.LightSalmon;
                }
                else if (dt.Rows[i][5].ToString() == "High")
                {
                    dataGridView1.Rows[i].Cells[5].Style.BackColor = Color.OrangeRed;
                }
                else if (dt.Rows[i][5].ToString() == "Very high")
                {
                    dataGridView1.Rows[i].Cells[5].Style.BackColor = Color.Red;
                }
            }
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.ColumnIndex;
            if (i >0 && i<13)
            {
                frmUpdateUserTask.idtask_fr_taskmanagement = dataGridView1.CurrentRow.Cells["Column2"].Value.ToString();
                frmUpdateUserTask.username_Us_Ta = dataGridView1.CurrentRow.Cells["Column11"].Value.ToString();
                frmUpdateUserTask updateusertask = new frmUpdateUserTask();
                updateusertask.ShowDialog();
                Form1_Load(null, null);
            }
        }
    }
}
